<template>
  <el-dialog
    :title="formData.id?'编辑':'新增'"
    :visible="showDialog"
    width="30%"
    @close="closeFn"
  >
    <el-form ref="from" label-width="80px" :model="formData" :rules="formRules">
      <el-form-item label="角色名称" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="formData.description" type="textarea" />
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-row type="flex" justify="center">
        <el-button @click="closeFn">取 消</el-button>
        <el-button type="primary" @click="confirmFn">确 定</el-button>
      </el-row></span>
  </el-dialog>
</template>

<script>
import { addRoles, editRolesById, updateRoles } from '@/api/roles'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        title: '',
        description: ''
      },
      formRules: {
        title: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击 新增还是编辑
    async confirmFn() {
      this.$refs.from.validate()
      if (this.formData.id) {
        // 编辑
        const res = await updateRoles(this.formData)
        console.log(res, 54)
      } else {
        // 新增
        await addRoles(this.formData)
      }
      // 关闭
      this.closeFn()
      // 刷新列表
      this.$emit('updateEvent')
    },
    closeFn() {
      // 关闭弹窗
      this.$emit('update:showDialog', false)
      this.formData = {
        title: '',
        description: ''
      }
    },
    // 根据id获取指定角色
    async getRolesListById(id) {
      const res = await editRolesById(id)
      this.formData = res.data
      // console.log(this.formData, 732)
    }
  }
}
</script>

<style></style>
